﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bts_webapp.Models;


namespace bts_webapp
{
    public partial class UserCard : System.Web.UI.UserControl
    {
        public int CardId
        {
            get
            {
                if (ViewState["CardId"] != null)
                    return Convert.ToInt32(ViewState["CardId"].ToString());
                return 0;
            }
            set
            {
                ViewState["CardId"] = value;
            }
        }
        public string CardName
        {
            get
            {
                return this.txtCardName.Text;
            }
            set { this.txtCardName.Text = value; }
        }
        public string PortCard
        {
            get
            {
                return this.txtPortCard.Text;
            }
            set { this.txtPortCard.Text = value; }
        }
        public string CardDesc
        {
            get
            {
                return this.txtDescirtion.Text;
            }
            set { this.txtDescirtion.Text = value; }
        }
        public bool Status
        {
            get
            {
                return this.chkStatus.Checked;
            }
            set { this.chkStatus.Checked = value; }
        }
        public string ThietbiID
        {
            get
            {
                return this.comboThietbi.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_Thietbi(value); }
        }

        public bool InsertOrUpdate
        {
            set
            {
                this.btnUpdate.CommandName = value ? "PerformInsert" : "Update";
                this.btnUpdate.Text = value ? "Insert" : "Update";
            }
            get
            {
                return this.btnUpdate.Text == "Insert";
            }
        }

        public void EnableFieldCode(bool enabled)
        {
            this.txtCardCode.Enabled = enabled;
        }

        public void BindData(VIBA_CARD card)
        {
            this.CardId = card.Card_Id;
            this.CardName = card.Card_TenCard;
            this.CardDesc = card.Card_GhiChu;
            this.ThietbiID = card.ThietBi_Id;
            this.PortCard=Convert.ToString(card.Card_SoPort);
            this.Status = card.Status == 0 ? false : true;
            
        }
        public VIBA_CARD GetData()
        {
            BTSModelDataContext bts = new BTSModelDataContext();
           
            VIBA_CARD card = new VIBA_CARD
            {
                //Card_Id = this.CardId,
                //Card_TenCard = this.CardName,
                //Card_GhiChu = this.CardDesc,
                //Card_SoPort=Convert.ToInt32( this.PortCard),
                //ThietBi_Id=Convert.ToInt32( this.ThietbiID),
                //Status = GetStatus(this.Status),

                Card_Id=this.CardId,
                Card_TenCard=this.CardName,
                Card_SoPort=Convert.ToInt16(this.PortCard),
                Card_GhiChu =this.CardDesc,
                ThietBi_Id=this.ThietbiID,
                Status=GetStatus(this.Status)

            };

            return card;
        }
        private int GetStatus(bool status)
        {
            return status ? 1 : 0;
        }
        public void SetErrorStatus(string error)
        {
            if (error.Trim() != string.Empty)
                this.litStatus.Text = "<font color='red'>" + error + "</font>";
            else
                this.litStatus.Text = string.Empty;
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void UpdateFlow_Thietbi(string Thietbi_Id)
        {
            var item = this.comboThietbi.Items.FirstOrDefault(x => x.Value.Trim() == Thietbi_Id.Trim());
            if (item != null)
            {
                var index = this.comboThietbi.Items.IndexOf(item);
                this.comboThietbi.SelectedIndex = index;
            }
        }

        public void AddComboBoxItems_Thietbi()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_THIET_BIs;
                this.comboThietbi.DataSource = list;
                this.comboThietbi.DataTextField = "ThietBi_Ten";
                this.comboThietbi.DataValueField = "ThietBi_Id";
                this.comboThietbi.DataBind();
                this.UpdateFlow_Thietbi(this.ThietbiID);
            }
        }
    }
}